package com.lab.network.model;

import com.cuitrip.app.MainApplication;
import com.cuitrip.service.R;
import com.lab.logtrack.AppLog;
import com.lab.network.config.CtResponseCode;
import com.lab.network.util.NetworkUtil;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.ToastUtil;

import java.io.IOException;
import java.net.HttpURLConnection;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by ak on 15/11/16.
 */
public abstract class ResultCallback implements Callback {


    private ParseType parseType;


    public ResultCallback(ParseType type) {
        this.parseType = type;
    }

    @Override
    public void onFailure(Call call, IOException e) {
        try {
            CtApiResponse ctApiResponse = handleResponse(null);
            onResponse(ctApiResponse);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        CtApiResponse ctApiResponse = handleResponse(response);
        if (ctApiResponse.isTokenInvalid()) {
            return;
        }
        onResponse(ctApiResponse);
    }

    public CtApiResponse handleResponse(Response response) throws IOException {
        //网络失败
        if (response == null) {
            CtApiResponse ctResponse = new CtApiResponse();
            if (NetworkUtil.getConnectState(AppCoreRuntime.context) == NetworkUtil.CONNECT_NONE) {
                ctResponse.code = CtResponseCode.NETWORK_CONNECT_NONE;
                ctResponse.result = null;
                ctResponse.msg = MainApplication.getInstance().getString(R.string.feedback_no_internet_1);
            } else {
                ctResponse.code = CtResponseCode.DATA_ERROR;
                ctResponse.result = null;
                ctResponse.msg = MainApplication.getInstance().getString(R.string.feedback_data_err);
            }
            return ctResponse;
        }

        int responseCode = response.code();
        //HTTP正常
        if (responseCode == HttpURLConnection.HTTP_OK){
            CtApiResponse ctApiResponse = new CtApiResponse(parseType);
            ctApiResponse.parseResponse(response.body().string());
            response.body().close();
            if (parseType == null){
                return ctApiResponse;
            }
            if (ctApiResponse.isResponseNormal()){
                ctApiResponse.reSerializeData(parseType);
            }else if (ctApiResponse.isTokenInvalid()) {
                AppLog.d("request", "token isTokenInvalided");
                MainApplication.getInstance().logOutWithError();
            }
            return ctApiResponse;
        }else{
            CtApiResponse failResponse = new CtApiResponse();
            failResponse.code = CtResponseCode.DATA_ERROR;
            failResponse.result = null;
            failResponse.msg = MainApplication.getInstance().getString(R.string.feedback_data_err);
            ToastUtil.toast("" + responseCode);
            return failResponse;
        }
    }


    public abstract void onResponse(CtApiResponse ctApiResponse);



}
